package com.ikangtai.shecare.stickycalendar.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.stickycalendar.bean.MoreDayState;
import com.ikangtai.shecare.stickycalendar.fragment.CalendarRecordFragment;
import com.ikangtai.shecare.stickycalendar.utils.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/1/16 0016.
 */
public class WeekCalendarAdapter extends CalendarBaseAdpter {
    private List<View> views;
    private Context context;

    private Handler os = null;

    private Map<String, MoreDayState> mMap = new HashMap<>();
    private String strToday = "";

    public WeekCalendarAdapter(List<View> views, Context context, Map<String, MoreDayState> map) {
        this.mMap = map;
        this.views = views;
        this.context = context;
        //选中今天
        Calendar today = new GregorianCalendar();
        today.setTimeInMillis(System.currentTimeMillis());

        strToday = DateUtils.getTagTimeStr(today);

        selectTime = DateUtils.getTagTimeStr(today);
    }

    public void setHandler(Handler os) {
        this.os = os;
    }

    @Override
    public int getCount() {
        return 9600;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView(views.get(position % views.size()));
    }

    public void getTimeList(Map<String, MoreDayState> map) {
        this.mMap = map;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ViewGroup view = (ViewGroup) views.get(position % views.size());
        int index = container.indexOfChild(view);
        if (index != -1) {
            container.removeView(view);
        }
        try {
            container.addView(view);
        } catch (Exception e) {

        }

        //给view 填充内容

        //设置开始时间为本周日
        Calendar today = new GregorianCalendar();
        today.setTimeInMillis(System.currentTimeMillis());
        int day_of_week = today.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        today.add(Calendar.DATE, -day_of_week);
        //距离当前时间的周数
        int week = getCount() / 2 - position;
        today.add(Calendar.DATE, -week * 7);

        render(view, today);
        return view;
    }

    /**
     * 渲染page中的view：7天
     */
    private void render(final ViewGroup view, final Calendar today) {
        for (int a = 0; a < 7; a++) {
            final int dayOfMonth = today.get(Calendar.DAY_OF_MONTH);
            // int day_of_year=today.get(Calendar.DAY_OF_YEAR);
            final ViewGroup dayOfWeek = (ViewGroup) view.getChildAt(a);
            //((TextView) dayOfWeek.getChildAt(0)).setText(getStr(today.get(Calendar.DAY_OF_WEEK)));
            ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setText(dayOfMonth + "");

            dayOfWeek.findViewById(R.id.imv_point).setVisibility(View.INVISIBLE);
            dayOfWeek.setTag(DateUtils.getTagTimeStr(today));

            dayOfWeek.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectTime = dayOfWeek.getTag().toString();

                    is = true;
                    //TODO:发消息，告诉Activity我改变选中的日期了
                    if (WeekCalendarAdapter.this.os != null) {
                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("selectTime", selectTime);
                        message.setData(bundle);
                        message.what = CalendarRecordFragment.selectTime;
                        os.sendMessage(message);
                        os.sendEmptyMessage(CalendarRecordFragment.change);
                    }
                    today.add(Calendar.DATE, -7);//因为已经渲染过7天，所以today往前推7天， 代表当前page重绘；

                    //更改非当前日的颜色
                    if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getSelectorBg()));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_translate));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable((R.drawable.re_circle_bg_white)));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_translate));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
                    }
                    //将上一个选中的day的状态恢复
                    if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getNormalBg()));
                        ((TextView) day.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        ((TextView) day.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
                    }
                    //特殊情况:上个选中的day今天
                    if (strToday.equals(tag) && mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(strToday).getSelectorBg()));
                        day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_translate));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                    }


                    //显示的调用invalidate
                    dayOfWeek.invalidate();

                    render(view, today);

                    is = false;

                }
            });

            if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                dayOfWeek.findViewById(R.id.imv_point).setVisibility(mMap.get(DateUtils.getTagTimeStr(today)).isFuck()
                        ? View.VISIBLE : View.INVISIBLE);
            }
            //事实今天

            if (strToday.equals(DateUtils.getTagTimeStr(today))) {
//                if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
//                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getSelectorBg()));
//                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_translate));
//                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
//                } else {
//                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getSelectorBg()));
//                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
//                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
//                }
//                if (!selectTime.equals(strToday)) {
//                    today.add(Calendar.DATE, 1);
//                    continue;
//                }
//            } else {
                if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(
                            mMap.get(strToday).getNormalBg()));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(
                            mMap.get(strToday).getNormalTextColor()));
                } else {
                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(
                            R.drawable.re_circle_bg_white));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(
                            R.color.black));
                }

            }
            //选中今天


            if (selectTime.equals(DateUtils.getTagTimeStr(today))) {
                if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(
                            mMap.get(DateUtils.getTagTimeStr(today)).getSelectorBg()));
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable((R.drawable.re_circle_bg_translate)));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                } else {
                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(
                            R.drawable.re_circle_bg_white));
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
                }
                day = dayOfWeek;
                tag = selectTime;
            } else {
                if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getNormalBg()));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(mMap.get(DateUtils.getTagTimeStr(today)).getNormalTextColor()));
                } else {
                    dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
                }
//

            }
            today.add(Calendar.DATE, 1);
        }

    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    private ViewGroup day = null;
    private String tag = "";

    public String getSelectTime() {
        return selectTime;
    }

    public void setSelectTime(String selectTime) {
        this.selectTime = selectTime;
    }


}
